<?php
class CatModel extends Model {
	protected $tableName = 'product_category_tag';
	/**
	*获取分类信息
	*@date 2013-9-26
	*@time 下午3:56:33
	* @param number $cid 分类ID
	* @return multitype:|Ambigous <mixed, boolean, NULL, multitype:, unknown, string>
	*/
	function info($cid=0) {
	//获取分类信息
		$res=array();
		if($cid==0){
			return $res;
		}
		$res=$this->find($cid);
		return $res;
	}//end info
	
	/**
	*获取分类关联的属性组及属性值
	*@date 2013-9-26
	*@time 下午3:59:03
	*@update by tb 2013.12.30
	* @param number $cid 分类ID
	* @param string $platform 平台信息 ALL SMW01 91YUNGOU
	* @param number $status 0 1 状态
	* @param string $cat 返回数据类型 attr（属性）|list（列表带分页）
	* @param string $limit 分页
	* @return number|Ambigous <mixed, string, boolean, NULL, unknown, multitype:, multitype:multitype: , void, object>|multitype:Ambigous <unknown, multitype:>
	*/
	function cat_attr($cid=0,$platform='ALL',$status=1,$cat='attr',$limit='',$order='id desc') {
	//获取关联的属性组
		$catdao=M('CategoryAttribute');
		$where=array();
		if($cid){
			$where['category_id']=$cid;
		}
		$platform=strtoupper($platform);
		if($platform!='ALL'){
			$platformstr=array();
			if(strpos($platform,',')!==false){
				$platforms=explode(',', $platform);
				foreach ($platforms as $v){
					$platformstr[]=' FIND_IN_SET('.$v.',`platform`)>0 ';
				}
				$platformstr=implode('OR', $platformstr);
			}else{
				$platformstr=' FIND_IN_SET('.$platform.',`platform`)>0 ';
			}
			$where['_String']=$platformstr;
		}
		$res=$catdao->where($where)->order('sequence DESC ,attr_id')->order($order)->limit($limit)->select();
		if(empty($res)){
			return 0;
		}
		//获取属性信息
		$aids=arr_to_explode($res,'attr_id',',');
		$attrdao=M('Attribute');
		$where=array();
		if($status!==false){
			$where['status']=$status;
		}
		$where['id']=array('IN',$aids);
		$attr=$attrdao->where($where)->select();
		if(empty($attr)){
			return 0;
		}
		
		//获取分类信息
		$cids=empty($cid) ? arr_to_explode($res,'category_id',',') : $cid;
		$catedao=M('ProductCategoryTag');
		$where['id']= empty($cid) ? array('IN',arr_to_explode($res,'category_id',',')) : $cid;
		$cates = $catedao->where($where)->select();
		$cates = field2key($cates, 'id');
		
		if($cat=='attr'){
			return $attr;
		}elseif($cat=='list'){
			$attr=field2key($attr,'id');
			$temp=array();
			foreach ($res as $v){
				$v['attr']=$attr[$v['attr_id']];
				$v['catename'] = $cates[$v['category_id']]['name'];
				$temp[]=$v;
			}
			
			return $temp;
		}
	}//end cat_attr
	
	/**
	*查询属性组下的属性
	*@date 2013-9-26
	*@time 下午6:26:30
	*/
	function group_attr($gid=0) {
	//查询属性组下的属性
		$group=M('AttributeGroup');
		$aids=$group->field('attr_ids')->find($gid);
		$aids=$aids['attr_ids'];
		if(empty($aids)){
			return 0;
		}
		$attrdao=M('Attribute');
		$where=array();
		$where['status']=1;
		$where['id']=array('IN',$aids);
		$attr=$attrdao->where($where)->select();
		return $attr;
	}//end group_attr
}